// libs/WXYHNav/WXYHNav.js
const YHTools = require("../WXYHTools/YHTools.js")
const yhTools = new YHTools();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        stateBarheight: {
            type: Number,
            value: wx.getSystemInfoSync().statusBarHeight
        },
        YHNavLeftBtns: {
            type: Array,
            value: [{
                src: "https://micro-program.oss-cn-beijing.aliyuncs.com/cskx/icon/account/%E8%BF%94%E5%9B%9E.png",
                action: function () {
                    wx.navigateBack()
                },
                title:"",
                style: "width:40px;height:40px;padding: 10px 15px 10px 5px;"
            }],
            observer(newV, oldV) {
                newV.forEach(element => {
                    if (element.title && !!element.title) {
                        element.src = ''
                    }
                });
                this.setData({
                    leftBtnInfos: newV
                });
            }
        },
        YHNavBars: {
            type: Array,
            value: [{
                title: "评价",
                action: () => {
                    console.log('评价')
                },
                style: ""
            }]

        },
        YHNaStyle: {
            type: Array,
            value: [""],
            observer(newVal, oldVal) {
                if (newVal) {
                    let newSets = [],
                        sets = [];
                    for (const iterator of newVal) {
                        if (iterator == "") {
                            continue
                        };

                        for (let index = 0; index < oldVal.length; index++) {
                            const element = oldVal[index];
                            if (element != "" && element.trim() != "") {
                                const ek = element.split(":")[0],
                                    ik = iterator.split(":")[0];
                                if (element == "") {
                                    continue
                                };
                                if (ek == ik) {
                                    sets.push({
                                        index: index,
                                        value: element
                                    })
                                } else {
                                    newSets.push(iterator);
                                }
                            } else {
                                newSets.push(iterator);
                            }

                        }
                    }

                    for (const iterator of sets) {
                        oldVal = oldVal.splice(iterator.index, 1, iterator.value);
                    }
                    let newValstr = oldVal.concat(newSets).slice(1).join(";") + ";";

                    this.setData({
                        naStyle: newValstr
                    })
                }
            }
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        naStyle: "",
        leftBtnInfos: [{
            src: "https://micro-program.oss-cn-beijing.aliyuncs.com/cskx/icon/account/%E8%BF%94%E5%9B%9E.png",
            action: function () {
                wx.navigateBack()
            },
            style: ""
        }]
    },

    /**
     * 组件的方法列表
     */
    methods: {
        tapLeftBtns: function (e) {

            if (yhTools.checkMultipleAction({
                page: this,
                event: 'tapLeftBtns'
            })) {
                return;
            }

            let index = e.currentTarget.dataset.index;

            let info = this.data.leftBtnInfos[index];
            if ("action" in info) {
                info.action();
            }
        },
        naviewbarAction: function (e) {
            if (yhTools.checkMultipleAction({
                page: this,
                event: 'naviewbarAction'
            })) {
                return;
            }
            const that = this,
                index = e.currentTarget.dataset.index;
            let b = that.data.YHNavBars[index];
            b.action();
        }
    }
})